package io.renren.modules.communityService.service.impl;

import io.renren.common.validator.Assert;
import io.renren.modules.communityService.dto.SysUserLoginDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.communityService.dao.SysUserDao;
import io.renren.modules.communityService.entity.SysUserEntity;
import io.renren.modules.communityService.dto.SysUserDTO;
import io.renren.modules.communityService.vo.SysUserVO;
import io.renren.modules.communityService.service.SysUserService;
import io.renren.modules.communityService.conver.SysUserConver;

import java.io.Serializable;
import java.util.Collection;


@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Override
    public PageUtils<SysUserVO> queryPage(SysUserDTO sysUser) {
        IPage<SysUserEntity> page = baseMapper.selectPage(
                new Query<SysUserEntity>(sysUser).getPage(),
                new QueryWrapper<SysUserEntity>()
        );

        return PageUtils.<SysUserVO>page(page).setList(SysUserConver.MAPPER.conver(page.getRecords()));
    }
    @Override
    public SysUserVO getById(Long userId) {
        return SysUserConver.MAPPER.conver(baseMapper.selectById(userId));
    }

    @Override
    public boolean save(SysUserDTO sysUser) {
        SysUserEntity sysUserEntity = SysUserConver.MAPPER.converDTO(sysUser);
        return this.save(sysUserEntity);
    }

    @Override
    public boolean updateById(SysUserDTO sysUser) {
        SysUserEntity sysUserEntity = SysUserConver.MAPPER.converDTO(sysUser);
        return this.updateById(sysUserEntity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }

    @Override
    public SysUserVO login(SysUserLoginDTO sys) {
        Assert.isBlank(sys.getUsername(),"用户名不能为空");
        Assert.isBlank(sys.getPassword(),"密码不能为空");

        SysUserEntity one = this.getOne(new QueryWrapper<SysUserEntity>().lambda()
                .eq(SysUserEntity::getUsername,sys.getUsername())
                .eq(SysUserEntity::getPassword,sys.getPassword())
        );
        Assert.isNull(one,"账号或者密码错误");
        return SysUserConver.MAPPER.conver(one);
    }

}
